<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="资源标题不能为空" placeholder="请输入标题" value="" class="layui-input" id="title-input">
                <tip>填写班级的名称，如 19-3数据科学与大数据技术。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">资源简介</label>
            <div class="layui-input-block">
                <textarea name="describe" autocomplete="off" placeholder="请输入资源简介" class="layui-input" id="describe-input"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">资源标签</label>
            <div class="layui-input-block">
                <select name="city" lay-verify="" lay-search id="tag-select">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否公开</label>
            <div class="layui-input-block">
                <input type="checkbox" name="isPublic" lay-skin="switch" lay-text="是|否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拍摄日期</label>
            <div class="layui-input-block">
                <input type="text" name="date" id="date" lay-verify="date" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">拍摄地点</label>
            <div class="layui-input-block">
                <input type="text" name="location" lay-verify="location" autocomplete="off" placeholder="请输入拍摄地点"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$,
            laydate = layui.laydate;

        //初始化表单，要加上，不然刷新部分组件可能会不加载
        form.render();
        //日期
        laydate.render({
            elem: '#date'
        });

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var isPub = true;
            if (data.field.isPublic === 'off') {
                isPub = false;
            }
            console.log(jq1111("#upload-file"))
            jq1111.ajax({
                type: "POST",
                crossDomain: true,
                url: BasePath+"api/Resource/Update",
                contentType: "application/json",
                dataType: "json",
                xhrFields: {
                    withCredentials: true
                },
                data:JSON.stringify({
                    "id": resId,
                    "description": data.field.describe,
                    "title": data.field.title,
                    "isPublic": isPub
                }),
                success: function (message) {
                    console.log(message);
                    var index=layer.alert(message.msg, {
                        title: '操作结果'
                    },function (){
                        // 关闭弹出层
                        layer.close(index);
                        layer.close(parentIndex);
                    })
                },
                error: function (message) {
                    console.log(message.responseJSON);
                    layer.alert(message.responseJSON.msg, {
                        title: '操作结果'
                    })
                },
            });

            return false;
        });

        //获取所有TAG
        var tagData;
        jq1111.ajax({
            type: "GET",
            crossDomain: true,
            url: BasePath+"api/ResourceTag/",
            processData: false,
            contentType: false,
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            success: function (message) {
                console.log(message);
                tagData=message.data;
                tagData.forEach(function (tag) {
                    jq1111("#tag-select").append(
                        '<option value="'+tag.id+'">'+tag.name+'</option>'
                    )
                });
                form.render();
            },
            error: function (message) {
                console.log(message.responseJSON);
            },
        });

        //获取资源信息
        var resId=localStorage.getItem('selectedResId');
        // var resType=localStorage.getItem('selectedResType');
        jq1111.ajax({
            type: "GET",
            crossDomain: true,
            url: BasePath+"api/Resource/"+resId+"/Details",
            processData: false,
            contentType: false,
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            success: function (message) {
                console.log(message);
                jq1111("#title-input").val(message.data.resourceTitle)
                jq1111("#describe-input").val(message.data.resourceDescription)
                form.render();
            },
            error: function (message) {
                console.log(message.responseJSON);
            },
        });



    });

</script>